package com.atguigu.daijia.driver.controller;

import com.atguigu.daijia.common.login.GuiguLogin;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.util.AuthContextHolder;
import com.atguigu.daijia.driver.service.OrderService;
import com.atguigu.daijia.model.form.map.CalculateDrivingLineForm;
import com.atguigu.daijia.model.form.order.OrderFeeForm;
import com.atguigu.daijia.model.form.order.StartDriveForm;
import com.atguigu.daijia.model.form.order.UpdateOrderCartForm;
import com.atguigu.daijia.model.vo.base.PageVo;
import com.atguigu.daijia.model.vo.map.DrivingLineVo;
import com.atguigu.daijia.model.vo.order.CurrentOrderInfoVo;
import com.atguigu.daijia.model.vo.order.NewOrderDataVo;
import com.atguigu.daijia.model.vo.order.OrderInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name = "订单API接口管理")
@RestController
@RequestMapping("/order")
@SuppressWarnings({"unchecked", "rawtypes"})
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;

    /**
     * 查询订单状态
     * @param orderId
     * @return
     */
    @Operation(summary = "查询订单状态")
    @GuiguLogin
    @GetMapping("/getOrderStatus/{orderId}")
    public Result<Integer> getOrderStatus(@PathVariable("orderId") Long orderId) {
        Integer status = orderService.getOrderStatus(orderId);
        return Result.ok(status);
    }

    /**
     * 查询司机临时队列中的最新订单数据
     * @return
     */
    @Operation(summary = "查询司机临时队列中的最新订单数据")
    @GuiguLogin
    @GetMapping("/findNewOrderQueueData")
    public Result<List<NewOrderDataVo>> findNewOrderQueueData() {
        Long driverId = AuthContextHolder.getUserId();
        List<NewOrderDataVo> newOrderDataVoList = orderService.findNewOrderQueueData(driverId);
        return Result.ok(newOrderDataVoList);
    }

    /**
     * 查询司机执行中订单基本信息
     * @return
     */
    @Operation(summary = "查询司机执行中订单基本信息")
    @GuiguLogin
    @GetMapping("/searchDriverCurrentOrder")
    public Result<CurrentOrderInfoVo> searchDriverCurrentOrder() {
        Long driverId = AuthContextHolder.getUserId();
        CurrentOrderInfoVo currentOrderInfoVo = orderService.searchDriverCurrentOrder(driverId);
        return Result.ok(currentOrderInfoVo);
    }

    /**
     * 司机抢单
     * @param orderId
     * @return
     */
    @Operation(summary = "司机抢单")
    @GuiguLogin
    @GetMapping("/robNewOrder/{orderId}")
    public Result<Boolean> robNewOrder(@PathVariable("orderId") Long orderId) {
        Long driverId = AuthContextHolder.getUserId();
        Boolean result = orderService.robNewOrder(driverId, orderId);
        return Result.ok(result);
    }

    /**
     * 根据订单 ID 查询当前司机的订单信息
     * @param orderId
     * @return
     */
    @Operation(summary = "根据订单 ID 查询当前司机的订单信息")
    @GuiguLogin
    @GetMapping("/getOrderInfo/{orderId}")
    public Result<OrderInfoVo> getOrderInfo(@PathVariable("orderId") Long orderId) {
        Long driverId = AuthContextHolder.getUserId();
        OrderInfoVo orderInfoVo = orderService.getOrderInfo(driverId, orderId);
        return Result.ok(orderInfoVo);
    }

    /**
     * 计算接单司机到订单起始点最佳路线
     * @param calculateDrivingLineForm
     * @return
     */
    @Operation(summary = "计算接单司机到订单起始点最佳路线")
    @GuiguLogin
    @PostMapping("/calculateDrivingLine")
    public Result<DrivingLineVo> calculateRouteToOrderStartPoint(@RequestBody CalculateDrivingLineForm calculateDrivingLineForm) {
        DrivingLineVo drivingLineVo = orderService.calculateRouteToOrderStartPoint(calculateDrivingLineForm);
        return Result.ok(drivingLineVo);
    }

    /**
     * 接单司机到达代驾订单起始点
     * @return
     */
    @Operation(summary = "接单司机到达代驾订单起始点")
    @GuiguLogin
    @GetMapping("/driverArriveStartLocation/{orderId}")
    public Result<Boolean> driverArriveStartLocation(@PathVariable("orderId") Long orderId) {
        Long driverId = AuthContextHolder.getUserId();
        Boolean result = orderService.driverArriveStartLocation(orderId, driverId);
        return Result.ok(result);
    }

    /**
     * 更新代驾车辆信息
     * @param updateOrderCartForm
     * @return
     */
    @Operation(summary = "更新代驾车辆信息")
    @GuiguLogin
    @PostMapping("/updateOrderCart")
    public Result<Boolean> updateOrderCar(@RequestBody UpdateOrderCartForm updateOrderCartForm) {
        Long driverId = AuthContextHolder.getUserId();
        updateOrderCartForm.setDriverId(driverId);
        Boolean result = orderService.updateOrderCar(updateOrderCartForm);
        return Result.ok(result);
    }

    /**
     * 开始代驾服务
     * @param startDriveForm
     * @return
     */
    @Operation(summary = "开始代驾服务")
    @GuiguLogin
    @PostMapping("/startDrive")
    public Result<Boolean> startDrive(@RequestBody StartDriveForm startDriveForm) {
        Long driverId = AuthContextHolder.getUserId();
        startDriveForm.setDriverId(driverId);
        Boolean result = orderService.startDrive(startDriveForm);
        return Result.ok(result);
    }

    /**
     * 结束代驾服务
     * @param orderFeeForm
     * @return
     */
    @Operation(summary = "结束代驾服务")
    @GuiguLogin
    @PostMapping("/endDrive")
    public Result<Boolean> endDrive(@RequestBody OrderFeeForm orderFeeForm) {
        Long driverId = AuthContextHolder.getUserId();
        orderFeeForm.setDriverId(driverId);
        Boolean result = orderService.endDrive(orderFeeForm);
        return Result.ok(result);
    }

    /**
     * 分页查询司机代驾订单列表
     * @param page
     * @param limit
     * @return
     */
    @Operation(summary = "分页查询司机代驾订单列表")
    @GuiguLogin
    @GetMapping("/findDriverOrderPage/{page}/{limit}")
    public Result<PageVo> findDriverOrderPage(@PathVariable("page") Long page, @PathVariable("limit") Long limit) {
        Long driverId = AuthContextHolder.getUserId();
        PageVo pageVo = orderService.findDriverOrderPage(driverId, page, limit);
        return Result.ok(pageVo);
    }

    /**
     * 司机发送账单信息给乘客
     * @param orderId
     * @return
     */
    @Operation(summary = "司机发送账单信息给乘客")
    @GuiguLogin
    @GetMapping("/sendOrderBillInfo/{orderId}")
    public Result<Boolean> sendOrderBillInfo(@PathVariable("orderId") Long orderId) {
        Long driverId = AuthContextHolder.getUserId();
        Boolean result = orderService.sendOrderBillInfo(orderId, driverId);
        return Result.ok(result);
    }
}

